package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.constant.BompAppParamConstant;
import cc.rengu.igas.bomp.common.dao.FeeInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.FeeInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.FeeInfo;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.FeeRuleInfoBean;
import cc.rengu.igas.bomp.facade.bean.FeeTemplateInfoBean;
import cc.rengu.igas.bomp.facade.request.FeeTemplateListQueryRequest;
import cc.rengu.igas.bomp.facade.response.FeeTemplateListQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 费率模板列表查询服务
 * Edit by 赵奎伍 20200330
 */
public class FeeTemplateListQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        FeeTemplateListQueryRequest feeTemplateListQueryRequest = new FeeTemplateListQueryRequest();
        ConvertUtil.convertOutput(feeTemplateListQueryRequest);
        return feeTemplateListQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        FeeTemplateListQueryRequest feeTemplateListQueryRequest = (FeeTemplateListQueryRequest) request;
        FeeTemplateListQueryResponse feeTemplateListQueryResponse = new FeeTemplateListQueryResponse();
        /* 按照接口转换基础产品信息并响应给请求方 */
        BizResponse<FeeTemplateListQueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        BeanUtil.beanCopy(feeTemplateListQueryRequest, feeTemplateListQueryResponse);

        FeeInfoMapper feeInfoMapper = new FeeInfoMapperImpl();
        List<FeeInfo> feeInfoList = new ArrayList<>();

        String instId = feeTemplateListQueryRequest.getHeader().getInstId();
        String feeCd = feeTemplateListQueryRequest.getFeeTemplateCode();
        String feeName = feeTemplateListQueryRequest.getFeeTemplateName();
        int pageIndex = feeTemplateListQueryRequest.getPageIndex();
        int pageSize = feeTemplateListQueryRequest.getPageSize();
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageSize(pageSize);
        pageInfo.setPageIndex(pageIndex);
        feeTemplateListQueryResponse.setPageIndex(pageIndex);
        feeTemplateListQueryResponse.setPageSize(pageSize);
        if (!StringUtils.isEmpty(feeCd) || !StringUtils.isEmpty(feeName)) {

            if (-1 == pageIndex || -1 == pageSize) {
                /* 根据费率代码、费率名称、费率描述查询费率信息 */
                feeInfoList = feeInfoMapper.selectFeeInfoByKeys(instId, feeCd, feeName);
                if (CollectionUtils.isEmpty(feeInfoList)) {
                    rglog.error("根据查询条件查询费率模板列表查询服务为空!");
                    feeTemplateListQueryResponse.setTotalPage(BompAppParamConstant.INT_ZERO);
                    feeTemplateListQueryResponse.setTotalRows(BompAppParamConstant.INT_ZERO);
                    bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                    bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
                    bizResponse.setResult(feeTemplateListQueryResponse);
                    return bizResponse;
                }


                feeTemplateListQueryResponse.setTotalRows(feeInfoList.size());

            } else {
                PageResult pageResult = feeInfoMapper.selectFeeInfoPageInfoByKeys(instId, feeCd, feeName, pageInfo);
                if (null == pageResult || null == pageResult.getPageInfo() || null == pageResult.getResult()) {
                    rglog.error("根据查询条件查询费率模板列表查询服务pageResult 为null !");
                    feeTemplateListQueryResponse.setTotalPage(BompAppParamConstant.INT_ZERO);
                    feeTemplateListQueryResponse.setTotalRows(BompAppParamConstant.INT_ZERO);
                    bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                    bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
                    bizResponse.setResult(feeTemplateListQueryResponse);
                    return bizResponse;
                }

                /* 根据取出记录条数做对应处理 */
                if (pageResult.getResult().isEmpty()) {
                    rglog.error("根据查询条件查询费率模板列表查询服务为空!");
                    feeTemplateListQueryResponse.setTotalPage(BompAppParamConstant.INT_ZERO);
                    feeTemplateListQueryResponse.setTotalRows(BompAppParamConstant.INT_ZERO);
                    bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                    bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
                    bizResponse.setResult(feeTemplateListQueryResponse);
                    return bizResponse;
                } else {
                    for (Object object : pageResult.getResult()) {
                        feeInfoList.add((FeeInfo) object);
                    }
                    feeTemplateListQueryResponse.setTotalPage(pageResult.getPageInfo().getTotalPage());
                    feeTemplateListQueryResponse.setTotalRows(pageResult.getPageInfo().getTotalRows());
                }
            }

        } else {
            /* 根据多法人标识查询所有费率信息*/
            if (-1 == pageIndex || -1 == pageSize) {
                feeInfoList = feeInfoMapper.selectFeeInfoByInstId(instId);

                if (CollectionUtils.isEmpty(feeInfoList)) {
                    rglog.error("根据查询InstId:{}，查询费率模板列表查询服务为空!", instId);
                    feeTemplateListQueryResponse.setTotalPage(BompAppParamConstant.INT_ZERO);
                    feeTemplateListQueryResponse.setTotalRows(BompAppParamConstant.INT_ZERO);
                    bizResponse.setResult(feeTemplateListQueryResponse);
                    bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                    bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
                    return bizResponse;
                }
                feeTemplateListQueryResponse.setTotalRows(feeInfoList.size());

            } else {
                PageResult pageResult = feeInfoMapper.selectFeeInfoPageFeeInfoByInstId(instId, pageInfo);
                if (null == pageResult || null == pageResult.getPageInfo() || null == pageResult.getResult()) {
                    rglog.error("根据查询InstId:{}，查询费率模板列表查询服务pageResult 为null !", instId);
                    feeTemplateListQueryResponse.setTotalPage(BompAppParamConstant.INT_ZERO);
                    feeTemplateListQueryResponse.setTotalRows(BompAppParamConstant.INT_ZERO);
                    bizResponse.setResult(feeTemplateListQueryResponse);
                    bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                    bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
                    return bizResponse;
                }

                /* 根据取出记录条数做对应处理 */
                if (pageResult.getResult().isEmpty()) {
                    rglog.error("根据查询InstId:{}，查询费率模板列表查询服务为空!", instId);
                    feeTemplateListQueryResponse.setTotalPage(BompAppParamConstant.INT_ZERO);
                    feeTemplateListQueryResponse.setTotalRows(BompAppParamConstant.INT_ZERO);
                    bizResponse.setResult(feeTemplateListQueryResponse);
                    bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                    bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
                    return bizResponse;
                } else {
                    for (Object object : pageResult.getResult()) {
                        feeInfoList.add((FeeInfo) object);
                    }
                    feeTemplateListQueryResponse.setPageIndex(pageIndex);
                    feeTemplateListQueryResponse.setPageSize(pageSize);
                    feeTemplateListQueryResponse.setTotalPage(pageResult.getPageInfo().getTotalPage());
                    feeTemplateListQueryResponse.setTotalRows(pageResult.getPageInfo().getTotalRows());
                }
            }
        }


        if (!CollectionUtils.isEmpty(feeInfoList)) {
            /* 根据分页查询条件截取相关数据记录 */
            Map<String, FeeTemplateInfoBean> map = new HashMap<>();
            for (FeeInfo feeInfo : feeInfoList) {

                if (map.containsKey(feeInfo.getFeeCd())) {

                    FeeRuleInfoBean feeRuleInfoBean = getFeeRuleInfoBean(feeInfo);
                    FeeTemplateInfoBean feeTemplateInfoBean = map.get(feeInfo.getFeeCd());
                    feeTemplateInfoBean.getFeeRuleList().add(feeRuleInfoBean);

                } else {
                    FeeTemplateInfoBean feeTemplateInfoBean = new FeeTemplateInfoBean();
                    BeanUtil.beanCopy(feeInfo, feeTemplateInfoBean);
                    feeTemplateInfoBean.setFeeTemplateCode(feeInfo.getFeeCd());
                    feeTemplateInfoBean.setFeeTemplateName(feeInfo.getFeeName());
                    feeTemplateInfoBean.setFeeTemplateDesc(feeInfo.getFeeDesc());
                    FeeRuleInfoBean feeRuleInfoBean = getFeeRuleInfoBean(feeInfo);
                    List<FeeRuleInfoBean> feeRuleInfoBeanList = new ArrayList<>();
                    feeRuleInfoBeanList.add(feeRuleInfoBean);
                    feeTemplateInfoBean.setFeeRuleList(feeRuleInfoBeanList);
                    map.put(feeInfo.getFeeCd(), feeTemplateInfoBean);
                }
            }

            List<FeeTemplateInfoBean> feeTemplateInfoBeans = new ArrayList<>(map.values());
            /* 查询结果赋值 */
            feeTemplateListQueryResponse.setFeeTemplateInfoList(feeTemplateInfoBeans);
        } else {
            rglog.error("法人行<{}>还未配置费率模板信息!", feeTemplateListQueryRequest.getHeader().getInstId());
        }

        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(feeTemplateListQueryResponse);
        return bizResponse;
    }

    private FeeRuleInfoBean getFeeRuleInfoBean(FeeInfo feeInfo) {
        FeeRuleInfoBean feeRuleInfoBean = new FeeRuleInfoBean();
        BeanUtil.beanCopy(feeInfo, feeRuleInfoBean);
        feeRuleInfoBean.setFeeRuleId(feeInfo.getId());
        feeRuleInfoBean.setFeeRuleName(feeInfo.getFeeName());
        feeRuleInfoBean.setFeeIndex(String.valueOf(feeInfo.getpSeqNo()));
        feeRuleInfoBean.setFeeIntvMethod(feeInfo.getFeeIntvMethod());
        feeRuleInfoBean.setFeeStartValue(feeInfo.getpStartValue());
        feeRuleInfoBean.setFeeEndValue(feeInfo.getpEndValue());
        feeRuleInfoBean.setFeeType(feeInfo.getpCalType());
        feeRuleInfoBean.setFeeRate(feeInfo.getpFeeRate());
        feeRuleInfoBean.setFeeMaxAmt(feeInfo.getpFeeRateMax());
        feeRuleInfoBean.setFeeMinAmt(feeInfo.getpFeeRateMin());
        feeRuleInfoBean.setFeeFixedAmt(feeInfo.getpValue());
        feeRuleInfoBean.setStartDate(feeInfo.getStartDate());
        feeRuleInfoBean.setEndDate(feeInfo.getEndDate());
        return feeRuleInfoBean;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        FeeTemplateListQueryResponse feeTemplateListQueryResponse = (FeeTemplateListQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(feeTemplateListQueryResponse);
    }

    /**
     * 将数据库表结构对象转换成接口对象
     *
     * @param feeInfo              数据库表结构对象
     * @param feeTemplateInfoBeans 接口对象
     */
    private void convFeeInfo(FeeInfo feeInfo, List<FeeTemplateInfoBean> feeTemplateInfoBeans) {
        FeeTemplateInfoBean feeInfoBean = new FeeTemplateInfoBean();
        BeanUtil.beanCopy(feeInfo, feeInfoBean);
        if (feeInfo != null) {
            feeInfoBean.setFeeTemplateCode(feeInfo.getFeeCd());
            feeInfoBean.setFeeTemplateName(feeInfo.getFeeName());
            feeInfoBean.setFeeTemplateDesc(feeInfo.getFeeDesc());
        }
        feeTemplateInfoBeans.add(feeInfoBean);
    }

}
